import java.util.ArrayList;
import java.util.List;

public class BruteForceSolution {
    public List<Integer> findAnagrams(String s, String p) {
        int pLen = p.length();
        int[] arrP = new int[128];
        int sLen = s.length();
        for (int i = 0; i < pLen; i++) {
            char ch = p.charAt(i);
            arrP[ch]++;
        }
        List<Integer> ret = new ArrayList<>();
        for (int i = 0; i < sLen - pLen + 1; i++) {
            String sub = s.substring(i, i + pLen);
            int[] arrSub = new int[128];
            for (int j =  0; j < pLen; j++) {
                char ch = sub.charAt(j);
                arrSub[ch]++;
            }
            if (isSameArray(arrP, arrSub)) {
                ret.add(i);
            }
        }
        return ret;
    }

    private boolean isSameArray(int[] arr1, int[] arr2) {
        int n1 = arr1.length;
        int n2 = arr2.length;
        for (int i = 0; i < n1; i++) {
            if (arr1[i] != arr2[i]) {
                return false;
            }
        }
        return true;
    }
}
